package com.example.flutter_test_opengl_plugin

//import android.view.TextureView
//import com.example.flutter_test_opengl_plugin.surface_view.MySurfaceViewFactory
//import io.flutter.embedding.engine.plugins.FlutterPlugin
//import io.flutter.plugin.common.BinaryMessenger
//import io.flutter.plugin.common.MethodCall
//import io.flutter.plugin.common.MethodChannel
//import io.flutter.plugin.common.MethodChannel.MethodCallHandler
//import io.flutter.plugin.common.MethodChannel.Result
//import io.flutter.plugin.common.PluginRegistry.Registrar


import android.content.Context
import android.view.TextureView
import com.example.flutter_test_opengl_plugin.surface_view.MySurfaceViewFactory
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.platform.PlatformView
import io.flutter.plugin.platform.PlatformViewFactory



/** FlutterTestOpenglPlugin */
class FlutterTestOpenglPlugin : FlutterPlugin, MethodCallHandler {

    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private lateinit var channel: MethodChannel

    override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
//
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_test_opengl_plugin")
        channel.setMethodCallHandler(this)

//        channel.setMethodCallHandler { call, result ->
//            if (call.method == "createTextureView") {
//                val factory = MySurfaceViewFactory()
//                flutterPluginBinding.platformViewRegistry.registerViewFactory("opengl_view_long", factory)
//                result.success(null)
//            } else if (call.method == "getPlatformVersion") {
//                result.success("Android ${android.os.Build.VERSION.RELEASE}")
//            } else {
//                result.notImplemented()
//            }
//        }

        flutterPluginBinding
            .getFlutterEngine()
            .getPlatformViewsController()
            .getRegistry()
            .registerViewFactory(
                "opengl_view_long",
                MySurfaceViewFactory()
            )
    }

    override fun onMethodCall(call: MethodCall, result: Result) {
        if (call.method == "getPlatformVersion") {
            result.success("Android ${android.os.Build.VERSION.RELEASE}")
        } else {
            result.notImplemented()
        }
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }
}
